﻿
@{
    ViewBag.Title = "Index";
    var items = EShopV20.Models.ShoppingCart.Cart.Items;
}

<h2>Shopping Cart</h2>
@using (Html.BeginForm())
{
    <table class="table">
        <tr>
            <th>Name</th>
            <th>Unit Price</th>
            <th>Quantity</th>
            <th>Amount</th>
            <th></th>
        </tr>
        @foreach (var p in items)
        {
        <tr>
            <td>@p.Name</td>
            <td>@Html.FormatValue(p.UnitPrice, "$ {0:# ### ### ###.##0}")</td>
            <td><input name="@p.Id" value="@p.Quantity" size="3" /></td>
            <td>@Html.FormatValue(p.UnitPrice * p.Quantity * (1 - p.Discount), "$ {0:# ### ### ###.##0}")</td>
            <td>
                <a class="remove-from-cart" href="/Cart/Remove/@p.Id">
                    <img src="~/images/icons/Trash.png" />
                </a>
            </td>
        </tr>
        }
    </table>
    <div>
        <button onclick="form.action='/Cart/Update'" class="btn btn-default">Update</button>
        <button onclick="form.action='/Cart/Clear'" class="btn btn-default">Clear</button>
        <a class="btn btn-default" href="/Product/List">Continue</a>
        @if (!Request.IsAuthenticated)
        {
            <a onclick="{alert('Vui lòng đăng nhập'); return false}" 
               class="btn btn-default" href="#">Checkout</a>
        }
        else if (EShopV20.Models.ShoppingCart.Cart.Count == 0)
        {
            <a onclick="{alert('Vui lòng chọn hàng'); return false}" 
               class="btn btn-default" href="#">Checkout</a>
        }
        else
        {
            <a class="btn btn-default" href="/Order/Checkout">Checkout</a>
        }
    </div>
}


@section scripts{
    <script src="~/jquery-ui/jquery-ui.min.js"></script>
    <link href="~/jquery-ui/jquery-ui.min.css" rel="stylesheet" />
    <style id="cart-fly"></style>
    <script>
        $(function () {
            $("a.remove-from-cart").click(function () {
                $(this).parents("tr").hide(500);

                href = $(this).attr("href");
                $.ajax({
                    url: href,
                    data: {},
                    success: function (response) {
                        $("#cart-count").html(response.Count + " items");
                        $("#cart-amount").html("$ " + response.Amount);
                    }
                });
                return false;// hủy chức năng của liên kết
            });
        });
    </script>
}